<?php

/**
 * QPHP
 *
 * Copyright (c) 2022 QPHP
 *
 * @author lbnnbs
 * @copyright Copyright (c) 2022 lbnnbs QPHP (https://gitee.com/lbnnbs/qphp)
 * @license AGPLv3 License
 *
 * Session鉴权类
 */
class Auth {
    
    static private $_auth_config;
    static private $_auth_key = 'auth';
    
    /**
     * 写入鉴权相关数据
     */
    static private function _set($key, $value) {
        self::$_auth_config || self::$_auth_config = Sys::getCfg('auth');
        $key = self::$_auth_config['prefix'] . self::$_auth_key . '_' . $key;
        Session::set($key, $value);
    }
    
    /**
     * 读取鉴权相关数据
     */
    static private function _get($key) {
        self::$_auth_config || self::$_auth_config = Sys::getCfg('auth');
        $key = self::$_auth_config['prefix'] . self::$_auth_key . '_' . $key;
        return Session::get($key);
    }
    
    /**
     * 移除鉴权相关数据
     */
    static private function _remove($key) {
        self::$_auth_config || self::$_auth_config = Sys::getCfg('auth');
        $key = self::$_auth_config['prefix'] . self::$_auth_key . '_' . $key;
        Session::remove($key);
    }
    
    /**
     * 登录
     */
    static public function Login($uid, $scope = 'user') {
        self::_set('uid', MEncode($uid));
        self::_set('uscope', $scope);
        self::_set('uvalue', MEncode("{$scope}_{$uid}"));
    }
    
    /**
     * 退出登录
     */
    static public function Logout() {
        self::_remove('uid');
        self::_remove('uscope');
        self::_remove('uvalue');
    }
    
    /**
     * 鉴权，如果没有授权则带来源地址跳转到登录页
     */
    static public function checkAuthed($scope = 'user', $redirect = '') {
        
        if (self::_get('uscope') === $scope) {
            $uid = MDecode(self::_get('uid'));
            $uvalue = self::_get('uvalue');
            
            if ("{$scope}_{$uid}" === MDecode($uvalue)) {
                self::Login($uid, $scope);
                return;
            }
        }
        
        self::Logout();
        
        if (strpos($redirect, '/login') !== false) {
            $redirect = '';
        }
        
        $url = Sys::url('login', 'index', array('redirect' => $redirect));
        
        echo '<script type="text/javascript">location.replace("' . $url . '")</script>';
        exit(0);
    }
    
    /**
     * 是否已授权
     */
    static public function isAuthed($scope = 'user') {
        if (self::_get('uscope') === $scope) {
            $uid = MDecode(self::_get('uid'));
            $uvalue = self::_get('uvalue');
            if ("{$scope}_{$uid}" === MDecode($uvalue)) {
                self::Login($uid, $scope);
                return true;
            }
        }
        return false;
    }
    
    /**
     * 获取已授权用户ID
     */
    static public function getAuthedUid($scope = 'user') {
        if (self::_get('uscope') === $scope) {
            $uid = MDecode(self::_get('uid'));
            $uvalue = self::_get('uvalue');
            if ("{$scope}_{$uid}" === MDecode($uvalue)) {
                return MDecode(self::_get('uid'));
            }
        }
        return null;
    }
    
}
